'use strict';


import React, {Component} from 'react';

import {
    View,
    Image,
    ImageBackground,
    Dimensions,
    TouchableWithoutFeedback,
    StyleSheet,
    Text,
    TouchableOpacity,
    TextInput,
    ScrollView,
    TouchableHighlight,
    TouchableNativeFeedback,
    Switch,
    Platform,
    DatePickerIOS,
    DatePickerAndroid,
} from 'react-native';

import ToastUtil from '../../../../core/utils/toast-util';
import {Loading, EasyLoading} from 'react-native-easy-loading';
import DatePicker from 'react-native-datepicker'
import CameraButton from '../../../../core/component/comp.button-camera';
import Color from '../../../../core/constant/color';
import {NavigationActions} from 'react-navigation'
import Http from '../../../../core/utils/http';
import ToolbarSetProperty from '../../../../core/component/comp.toolbar-setproperty';
import SetTextInput from '../../../../core/component/comp.set-textInput';

let winWidth = Dimensions.get('window').width;//屏幕宽度
let winHeight = Dimensions.get('window').height;//屏幕高度
let headImgEmpty = require('shequnsiweiapp/src/img/headImg_empty.png');

export default class RegisterScene2 extends React.Component {
    // 构造
    constructor(props) {
        super(props);
        this.state = {
            nickName: '',
            sex: '',
            csrq: ''
        }
    }

    componentWillMount() {
    }

    submit() {
        Promise.resolve().then(EasyLoading.show('loading...', 5000));

        Http.post_upload('saveUserInfo', this.state.mobile, {
            touxiang: this.state.imageIndex,
            nickName: this.state.nickName,
            sex: this.state.sex,
            password: this.state.password,
            csrq: this.state.csrq,
            username: this.state.mobile,
            operateType: this.state.operateType,

        }).then((res) => {
            Promise.resolve().then(EasyLoading.dismis());
            ToastUtil.show(res.returnType.resultMessage);
            if (res.returnType.resultCode === '1') {
                if ('add' === this.state.operateType) {
                    //清空路由
                    let resetAction = NavigationActions.reset({
                        index: 0,
                        actions: [
                            NavigationActions.navigate({routeName: 'Login'})//要跳转到的页面名字
                        ]
                    });
                    this.props.navigation.dispatch(resetAction);


                } else {
                    this.props.navigation.goBack();

                }

            }
            // this.props.navigation.navigate('Login');

        }).catch((error) => {
            //alert(error);
            ToastUtil.show("报错:" + error);
            Promise.resolve().then(EasyLoading.dismis());
        });
    }


    componentDidMount() {
        EasyLoading.show('loading...', 5000);
        const {params} = this.props.navigation.state;
        let {mobile, password, operateType} = params;
        //  ToastUtil.show('mobile'+mobile);
        this.setState({
            mobile: params.mobile,
            password: params.password,
            operateType: params.operateType
        })

        if ('add' === operateType) {
            EasyLoading.dismis();
        } else {
            Http.post_dataObj('userinfo', mobile, {})
                .then(res => {
                    // ToastUtil.show(res.userinfo.length);

                    if (res.userinfo && res.userinfo.length > 0) {
                        let userinfo = res.userinfo[0];
                        // ToastUtil.show('SFZH:'+userinfo.SFZH);
                        this.setState({
                            xm: userinfo.NICK_NAME,
                        });
                    }
                    EasyLoading.dismis();
                }).catch(err => {
                //请求失败
                ToastUtil.show('发送失败，错误信息:' + err);
                EasyLoading.dismis();
            })
        }
    }


    sexSelect(value) {

    }

    setTouxiang() {
        const that = this;
        this.props.navigation.navigate("Touxiang", {
            operatorType: "add",
            imageIndex: this.state.imageIndex,
            touxiangUrl: this.state.touxiangUrl,
            callback(imageIndex, touxiangUrl) {
                that.setState({
                    imageIndex: imageIndex,
                    touxiangUrl: touxiangUrl
                })


            }
        })
    }


    setTextInput(title, property) {
        const that = this;
        this.props.navigation.navigate('SetTextInput', {
            title: title, oldValue: this.state[property], callback(value) {
                that.setState({
                    [property]: value
                })
            }
        })

    }


    setDataSource(title, property) {
        const that = this;
        this.props.navigation.navigate('SetDataSource', {
            title: title, oldValue: that.state[property]
            , dataSource: ['男', '女'], callback(value) {
                that.setState({
                    [property]: value
                })
            }
        })

    }

    setCsrq(title, property) {
        const that = this;
        this.props.navigation.navigate('SetDate', {
            title: title, oldValue: that.state[property]
            , callback(value) {
                that.setState({
                    [property]: value
                })
            }
        })
    }

    render() {
        let submitEnable = this.state.imageIndex && this.state.nickName && this.state.sex && this.state.csrq;
        return (
            <ScrollView style={styles.body}>
                <Loading/>
                <TouchableHighlight style={[styles.input_view, {height: 80}]} onPress={this.setTouxiang.bind(this)}>
                    <View style={{flexDirection: "row", flex: 1}}>
                        <View style={styles.input_label_view}><Text style={styles.input_label_text}>头像</Text></View>
                        <View style={styles.input_value_view}>
                            {
                                this.state.imageIndex
                                    ? <Image source={{uri: this.state.touxiangUrl}} style={{width: 80, height: 80}}/>
                                    : <Text style={styles.zhushi_text}>请设置</Text>

                            }
                            <Image style={{alignSelf: "center", marginLeft: 2}} source={require('shequnsiweiapp/src/img/arrows.png')}/>
                        </View>

                    </View>
                </TouchableHighlight>
                <TouchableHighlight style={styles.input_view} onPress={this.setTextInput.bind(this, '设置名字', 'nickName')}>
                    <View style={{flexDirection: "row", flex: 1}}>
                        <View style={styles.input_label_view}><Text style={styles.input_label_text}>昵称</Text></View>
                        <View style={styles.input_value_view}>
                            <Text style={styles.zhushi_text}>{this.state.nickName ? this.state.nickName : '请设置'}</Text>
                            <Image source={require('shequnsiweiapp/src/img/arrows.png')}/>
                        </View>

                    </View>
                </TouchableHighlight>
                <TouchableHighlight style={styles.input_view} onPress={this.setDataSource.bind(this, '设置性别', 'sex')}>
                    <View style={{flexDirection: "row", flex: 1}}>
                        <View style={styles.input_label_view}><Text style={styles.input_label_text}>性别</Text></View>
                        <View style={styles.input_value_view}>
                            <Text style={styles.zhushi_text}>{this.state.sex ? this.state.sex : '请设置'}</Text>
                            <Image source={require('shequnsiweiapp/src/img/arrows.png')}/>
                        </View>
                    </View>
                </TouchableHighlight>
                <TouchableHighlight style={styles.input_view} onPress={this.setCsrq.bind(this, '设置出生日期', 'csrq')}>
                    <View style={{flexDirection: "row", flex: 1}}>
                        <View style={styles.input_label_view}><Text style={styles.input_label_text}>出生日期</Text></View>
                        <View style={styles.input_value_view}>
                            <Text style={styles.zhushi_text}>{this.state.csrq ? this.state.csrq : '请设置'}</Text>
                            <Image source={require('shequnsiweiapp/src/img/arrows.png')}/>
                        </View>
                    </View>
                </TouchableHighlight>
                <View style={styles.zhushi_view}>
                    <Text style={styles.zhushi_text}>性别设置后不能修改，请谨慎操作</Text>
                </View>
                <TouchableHighlight onPress={this.submit.bind(this)}
                                    style={[styles.btn, {backgroundColor: submitEnable ? Color.BTN_BGCOLOR : Color.BTN_DISABLE_BGCOLOR}]}
                                    disabled={!submitEnable}>
                    <Text
                        style={[styles.btn_text, {color: submitEnable ? Color.BTN_TEXT : Color.BTN_DISABLE_TEXT}]}>完成</Text>
                </TouchableHighlight>
            </ScrollView>
        )
    }

}


const styles = StyleSheet.create({
    body: {
        backgroundColor: Color.VIEW_BGCOLOR,
        height: winHeight,
    },
    zhushi_view: {
        marginLeft: 15,
        marginRight: 15,
        marginTop: 15,
        marginBottom: 0,
        flexDirection: "row",
        justifyContent: "center",
    },
    zhushi_text: {
        fontSize: 14,
        color: Color.TEXT_GREY,
    },
    input_view: {
        padding: 8,
        marginLeft: 0,
        marginRight: 0,
        marginTop: 0,
        marginBottom: 0,
        flexDirection: "row",
        justifyContent: "center",
        backgroundColor: "#ffffff",
        borderBottomWidth: 1,
        borderColor: Color.TEXTINPUT_LINE
    },
    input_label_view: {
        flex: 1, flexDirection: "row",
    },
    input_label_text: {
        alignSelf: "center",
    },
    input_value_view: {
        flex: 3,
        alignSelf: "center",
        flexDirection: "row",
        justifyContent: "flex-end",
    },
    btn: {
        padding: 10,
        margin: 15,
        marginBottom: 0,
        borderRadius: 5,
        backgroundColor: Color.BTN_BGCOLOR,
    },
    btn_text: {
        fontSize: 15,
        alignSelf: 'center',
        color: Color.BTN_TEXT
    }
});

